/**
 * 
 */
package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.pojo.Student;

/**
 * 安徽省
 * 排名：完成
 * 去年排名:图片(未完成)
 * 
 * @author chenmin<chenmin@mouge.wang>
 * @review cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-24
 */
public class Anhui extends Exam
{

	public Anhui(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("1010113124");
		student.setIdCard("340302199602010440");
		Anhui guangxi = new Anhui(student);
		System.out.println(guangxi.login(""));
	}

	@Override
	public String login(String captcha)
	{
		Request conRequest = new Request("http://cx.ahzsks.cn/pugao/pgcj2015_in.php", mSha1);
		Document conDocument = Jsoup.parse(curlGet(conRequest));
		String captchas = conDocument.select("div[class=right]").get(2).text();

		Request loginRequest = new Request("http://cx.ahzsks.cn/pugao/pgcj2015_out.php", mSha1);
		loginRequest.addParams("zwh", mStudent.getStudentId());
		loginRequest.addParams("sfzh", mStudent.getIdCard());
		loginRequest.addParams("yzm", captchas);
		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("语文"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("history.go(-1)"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document resultDoc = Jsoup.parse(parseHtml);
		Elements tables = resultDoc.select("table");
		String info = tables.get(0).select("tr").get(0).select("td").get(0).html().replaceAll("&nbsp;", "");

		JSONObject json = new JSONObject();
		json.put("exam_id", regexMatch(info, "考生号：(.*)姓名"));
		json.put("name", regexMatch(info, "姓名：(.*)"));
		json.put("ranking", tables.get(0).select("tr").get(1).select("td").get(3).text());
		json.put("province", "安徽省");

		JSONObject score = new JSONObject();
		score.put("语文总成绩", tables.get(0).select("tr").get(3).select("td").get(1).text());
		score.put("语文主观题成绩", tables.get(0).select("tr").get(3).select("td").get(2).text());
		score.put("数学总成绩", tables.get(0).select("tr").get(4).select("td").get(1).text());
		score.put("数学主观题成绩", tables.get(0).select("tr").get(4).select("td").get(2).text());
		score.put("外语总成绩", tables.get(0).select("tr").get(5).select("td").get(1).text());
		score.put("外语主观题成绩", tables.get(0).select("tr").get(5).select("td").get(2).text());
		score.put("综合总成绩", tables.get(0).select("tr").get(6).select("td").get(1).text());
		score.put("综合主观题成绩", tables.get(0).select("tr").get(6).select("td").get(2).text());
		score.put("高考总分", tables.get(0).select("tr").get(1).select("td").get(1).text());
		score.put("全国性政策加分分值", tables.get(0).select("tr").get(7).select("td").get(1).text());
		score.put("地方性政策加分分值", tables.get(0).select("tr").get(8).select("td").get(1).text());
		json.put("score", score.toString());

		return json.toString();
	}
}
